package com.shijun.customview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.shijun.adapter.RecyclerWrapAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhushijun on 2017/12/29 0029.
 */

public class CustomRecyclerView extends RecyclerView{
    public static final int REFRESH_TYPE_INSIDE = 1001;//嵌套在里面的刷新动画
    public static final int REFRESH_TYPE_FLOAT = 1002;//浮动的刷新动画

    private List<View> headViews = new ArrayList<>();
    private List<View> footViews = new ArrayList<>();
    private Adapter mAdapter;
    private OnScrollTopOrBottomListener onScrollTopOrBottomListener;
    private OnScrollActionListener onScrollActionListener;

    private float touch_down_x;
    private float touch_down_y;
    private float touch_move_x;
    private float touch_move_y;
    private float touch_up_x;
    private float touch_up_y;

    public CustomRecyclerView(Context context) {
        super(context);
    }

    public CustomRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        if(onScrollActionListener!=null) {
            switch (e.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touch_down_x = e.getX();
                    touch_down_y = e.getY();
                    onScrollActionListener.onScrollDown(touch_down_x,touch_down_y);
                    break;
                case MotionEvent.ACTION_MOVE:
                    float touch_down_cur_x = e.getX();
                    float touch_down_cur_y = e.getY();
                    onScrollActionListener.onScrollMove(touch_down_x,touch_down_y,touch_down_cur_x,touch_down_cur_y);
                    break;
                case MotionEvent.ACTION_UP:
                    touch_down_x = e.getX();
                    touch_down_y = e.getY();
                    onScrollActionListener.onScrollUp(touch_down_x,touch_down_y);
                    break;
            }
        }
        return super.onTouchEvent(e);
    }




    public void setCustomHeadView(View view){
        headViews.add(view);
        if (mAdapter != null){
            if (!(mAdapter instanceof RecyclerWrapAdapter)){
                mAdapter = new RecyclerWrapAdapter(headViews,footViews,mAdapter) ;
            }
        }

    }

    public void setCustomFootView(View view){
        footViews.add(view);
        if (mAdapter != null){
            if (!(mAdapter instanceof RecyclerWrapAdapter)){
                mAdapter = new RecyclerWrapAdapter(headViews,footViews,mAdapter) ;
            }
        }
    }

    @Override
    public void setAdapter(Adapter adapter) {

        if (headViews.isEmpty()&&footViews.isEmpty()){
            super.setAdapter(adapter);
        }else {
            adapter = new RecyclerWrapAdapter(headViews,footViews,adapter) ;
            super.setAdapter(adapter);
        }
        mAdapter = adapter ;
    }


    @Override
    public void onScrolled(int dx, int dy) {
        super.onScrolled(dx, dy);
        if (!canScrollVertically(-1)) {
            onScrollTopOrBottomListener.onScrollTop();
        } else if (!canScrollVertically(1)||isScrollToBottom()) {
            onScrollTopOrBottomListener.onScrollBottom();
        }else{
            onScrollTopOrBottomListener.onScrollMiddle();
        }
    }

    public OnScrollTopOrBottomListener getOnScrollTopOrBottomListener() {
        return onScrollTopOrBottomListener;
    }

    public void setOnScrollTopOrBottomListener(OnScrollTopOrBottomListener onScrollTopOrBottomListener) {
        this.onScrollTopOrBottomListener = onScrollTopOrBottomListener;
    }

    protected boolean isScrollToBottom() {
        if (computeVerticalScrollExtent() + computeVerticalScrollOffset() >= computeVerticalScrollRange())
            return true;
        return false;
    }

    public OnScrollActionListener getOnScrollActionListener() {
        return onScrollActionListener;
    }

    public void setOnScrollActionListener(OnScrollActionListener onScrollActionListener) {
        this.onScrollActionListener = onScrollActionListener;
    }

    public interface OnScrollTopOrBottomListener{
        void onScrollTop();
        void onScrollBottom();
        void onScrollMiddle();
    }

    public interface  OnScrollActionListener{
        void onScrollDown(float startX,float startY);
        void onScrollMove(float startX,float startY,float currentX,float currentY);
        void onScrollUp(float startX,float startY);
    }
}
